if (!yiminghe)
    var yiminghe = {};
yiminghe.multiFileUploadField = Ext.extend(Ext.Panel, {
    layout:'fit',
    //文件编号Id的前缀
    startId:'multi_file_up',
    //最多上传文件数目
    limit:3,
    initComponent : function() {
    	//文件编号Name的前缀，用于向服务器提交
        if (!this.startName)
            this.startName = this.startId;

        this.items = {
            xtype:'hidden',
            value:'',
            name:this.startName + '_count',
            id:this.startId + '_count'
        };

        yiminghe.multiFileUploadField.superclass.initComponent.call(this);
        this.num = 0;

    },
    // private
    onRender : function(ct, position) {

        yiminghe.multiFileUploadField.superclass.onRender.call(this, ct, position);

    },

	//删除面板的全部文件框
    delAll:function() {
        var files = Ext.getCmp(this.startId + '_count').getValue();
        //alert('|' + files + '|');
        var items = files.split(' ');
        for (var i = 0; i < items.length; i++) {
            //alert(items[i]);
            if (items[i]) {
                this.remove(this.startId + '_column_' + items[i]);
                if (Ext.isIE) {
                    this.remove(this.startId + '_previewpanel_' + items[i]);
                }
            }
        }
        this.num = 0;
        Ext.getCmp(this.startId + '_count').setValue('');
    } ,
    //面板中是否有文件
    isEmpty:function() {
        var files = Ext.getCmp(this.startId + '_count').getValue();
        return  files.trim() == '';
    }   ,
    //向面板中加入文本框
    addFile:function() {

        var files = Ext.getCmp(this.startId + '_count').getValue().trim();
        if (files.split(' ').length >= this.limit) {
            alert('最多能上传 ' + this.limit + ' 个文件！');
            return;
        }

        this.num++;
        var curNum = this.num;
        Ext.getCmp(this.startId + '_count').setValue(Ext.getCmp(this.startId + '_count').getValue() + " " + curNum);
        var fileConfig = {};
        for (var p in this.fileConfig) {
            fileConfig[p] = this.fileConfig[p];
        }
        fileConfig.id = this.startId + '_file_' + curNum;
        fileConfig.name = this.startName + '_file_' + curNum;
        var startId = this.startId;
        fileConfig.listeners = {
            'fileselected' :function(c, v) {
                Ext.getCmp(startId + '_title_' + curNum).setValue(yiminghe.multiFileUploadField.getFileName(v));
                if (Ext.isIE) {
                    yiminghe.multiFileUploadField.preview(v, startId + '_previewpanel_' + curNum, startId + '_previewdiv_' + curNum);
                }
            }
        };

        var n = this.add({
            layout:'column',
            autoHeight:true,
            id:this.startId + '_column_' + curNum,
            items:[
                {
                    columnWidth:.5,
                    layout: 'form',
                    labelWidth:this.labelWidth,
                    items: [{
                        xtype: 'textfield',
                        inputType:'file',
                        fieldLabel: '文件名称'  ,
                        id:this.startId + '_title_' + curNum,
                        anchor:'95%',
                        name: this.startName + '_title_' + curNum
                    }]
                } ,
                {
                    columnWidth:.3,
                    layout: 'form',
                    labelWidth:this.labelWidth,
                    items: [ fileConfig]
                }  ,
                {
                    columnWidth:.2,
                    layout: 'form',

                    items: [ {
                        xtype:'button',
                        text:'清除',
                        handler:function() {
                            this.remove(n);
                            if (Ext.isIE) {
                                this.remove(Ext.getCmp(this.startId + '_previewpanel_' + curNum));
                            }
                            Ext.getCmp(this.startId + '_count').setValue(Ext.getCmp(this.startId + '_count').getValue().replace(' ' + curNum, ''));
                        },
                        scope:this
                    }]
                }

            ]
        });
        if (Ext.isIE) {
            this.add({
                xtype:'panel',
                html:'<div id="' + this.startId + '_previewdiv_' + curNum + '" ext:qtip="TODO" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( sizingMethod = scale );">' +
                     '</div><br><br>',
                title:'图片预览',
                collapsible:true,
                hidden:false,
                id:this.startId + '_previewpanel_' + curNum

            });
        }
        this.doLayout();
    }  ,
    afterRender : function() {
        yiminghe.multiFileUploadField.superclass.afterRender.call(this);

    }
});

yiminghe.multiFileUploadField.getFileName = function (file)
{
    var i = file.lastIndexOf("/");
    if (i == - 1)
    {
        i = file.lastIndexOf("\\");
    }
    var name = file;
    if (i != - 1)
    {
        name = file.substring(i + 1, file.length);
    }
    var j = name.lastIndexOf(".");
    if (j != - 1)
    {
        return name.substring(0, j);
    }
    else
    {
        return name;
    }
}


yiminghe.multiFileUploadField.getFileExt = function (file)
{
    var i = file.lastIndexOf(".");
    if (i == - 1)
    {
        return "";
    }
    var t = file.substring(i + 1, file.length);
    var j = t.lastIndexOf("/");
    if (j == - 1)
    {
        j = t.lastIndexOf("\\");
        if (j == - 1)
        {
            return t;
        }
    }
    return "";
}

yiminghe.multiFileUploadField.preview = function (fname, panel, div) {

    var fext = yiminghe.multiFileUploadField.getFileExt(fname).toLowerCase();
    if (fext == "gif" || fext == "bmp" || fext == "jpg" || fext == "jpeg" ||
        fext == "png") {
        //var obj =  o;

        Ext.getCmp(panel).body.hide();
        var newPreview = document.getElementById(div);
        newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = fname;
        newPreview.style.width = "400px";
        newPreview.style.height = "300px";
        Ext.getCmp(panel).show();
        Ext.getCmp(panel).body.slideIn('l', {stopFx:true,duration:.2});
    } else {
        Ext.getCmp(panel).hide();
    }
}
Ext.reg('multifileupload', yiminghe.multiFileUploadField);